var pageNum;
$(function () {
    initTime('beginTime','endTime','');

    getAgentUserList(1);
    $('body').on('click','#btn1, #btn2',function () {
        getAgentUserList(1);
    }).on('click','#new1',function () {//新建一级推广员
        initAddOrUpdateView(0, 1);
    }).on('click','#new2',function () {//新建二级推广员
        initAddOrUpdateView(0, 2);
    }).on('click', '.fa-arrow-up', function () {//提交
        var obj = $(this).parents('tr');
        updateStatus(obj, 2);
    }).on('click','.fa-unlock-alt', function () {//禁用
        var obj = $(this).parents('tr');
        updateStatus(obj, 4);
    }).on('click','.fa-history', function () {//恢复
        var obj = $(this).parents('tr');
        updateStatus(obj, 3);
    }).on('click','#lockBatch', function () {//批量禁用
        var obj = $('input.i-checks:checked').parents('tr');
        updateStatus(obj, 4);
    }).on('click','#regainBatch', function () {//批量恢复
        var obj = $('input.i-checks:checked').parents('tr');
        updateStatus(obj, 3);
    }).on('click','.fa-trash-o', function () {//删除
        var agentUserId = $(this).parents('tr').data('id');
        deleteAgentUser(agentUserId);
    }).on('click', '#importBatch', function () {
        window.location.href = baseloaction + '/admin/agent/user/initImportAgentUserView';
    }).on('click', '#exportBatch', function () {
        var url ='/admin/agent/user/downloadExcelAgentList';
        exportExecl.init({
            title:"推广员数据导出",
            excelUrl: url,
            type:9
        })
    }).on('click', '.fa-yen', function () {
        var agentUserId = $(this).parents('tr').data('id');
       /* // var fid = $(this).parents('tr');
        var obj = window.parent.frames.document.getElementById('J_menuTab_wrap');
        //var obj2 = window.parent.frames.document.getElementsByClassName('J_iframe ifrmae0')[0];
        var menuTabs = $(obj).find('.J_menuTab');
        menuTabs.each(function(index,val){
            var that =$(val);
            that.removeClass("active");
            if(that.data("id")=="/admin/agent/income/queryAgentIncomeList"){
                that.addClass("active");
            }
        });
        window.location.href = baseloaction + '/admin/agent/income/queryAgentIncomeInfo?agentUserId='+ agentUserId;*/

        var index = parent.layer.open({
            type: 2,
            shadeClose: false,
            shade: [0.3, '#000000'],
            title: '<strong>查看收益详情</strong>',
            area: winArea,
            content: baseloaction + '/admin/agent/income/queryAgentIncomeInfo?agentUserId='+ agentUserId,
            btn: ['关闭'],
            yes: function (index, layero) {
                parent.layer.close(index);
            }
        });

    });

    /**
     * 查询推广员列表
     * @param pageNo
     */
    function getAgentUserList(pageNo) {
        var params = $("#search-form").serializeJSON();
        params = JSON.stringify(params);
        var obj = {
            url: baseloaction + '/admin/agent/user/ajaxQueryAgentUserList',//请求URL
            contextId: 'agentUser-list-context',//响应内容页面设置内容ID
            params: params,//请求参数
            pageNo: pageNo,//页码
            pageCall: getAgentUserList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
            selectAllId: 'all-agentUser', //全选框ID
            callback: function (pageNo) {//数据加载完成回调
                $("#count").html(total);
                pageNum = pageNo;
            }
        };
        ajaxGetListData(obj);
    }

    /**
     * 去往新建页面
     * @param agentUserId
     * @param userType
     */
    function initAddOrUpdateView(agentUserId, userType) {
        if(userType == 1){
            window.location.href = baseloaction + "/admin/agent/user/initAdd1?agentUserId="+ agentUserId + "&userType=" + userType;
        }else if(userType == 2){
            window.location.href = baseloaction + "/admin/agent/user/initAdd2?agentUserId="+ agentUserId + "&userType=" + userType;
        }
    }

    //修改状态
    function updateStatus(obj, status) {
        var message = "";
        if(status == 2){
            message = "提交";
        }else if(status == 3){
            message = "恢复";
        }else if(status == 4){
            message = "禁用";
        }

        if($.isEmptyObject(obj) || obj.length <= 0){
            icModale(0, '请选择要'+ message +'的数据');return;
        }

        icModale(2,"确定要"+ message +"所选的推广员吗?",3,function () {
            var agentUserIds = obtainIds(obj);
            $.ajax({
                url: baseloaction + '/admin/agent/user/updateStauts',
                data:{"agentUserIds": agentUserIds, "status":status},
                type:"post",
                dataType:"json",
                success: function (result) {
                    icModale(0, result.message);
                    if(result.success){
                        if(status == 2){
                            obj.find('td[agentUserStatus]').html('<span class="label label-primary">待审核</span>');
                            obj.find('a[agentUserStatus1]').remove();
                            obj.find('a[agentUserStatus2]').show();
                            obj.find('a[agentUserStatus5]').remove();
                        }else if(status == 3){
                            obj.find('td[agentUserStatus]').html('<span class="label label-info">正常</span>');
                            obj.find('a[agentUserStatus]').attr('data-original-title','禁用').html('<i class="fa fa-unlock-alt"></i>');
                        }else if(status == 4){
                            obj.find('td[agentUserStatus]').html('<span class="label label-danger">禁用</span>');
                            obj.find('a[agentUserStatus]').attr('data-original-title','恢复').html('<i class="fa fa-history"></i>');
                        }
                    }
                },
                error: function (error) {
                    icModale(0, '系统繁忙，数据'+ message +'失败');
                }
            })
        });

    }

    /**
     * 删除推广员
     * @param agentUserId
     */
    function deleteAgentUser(agentUserId) {
        var rowNum = $('tr.rowNum').length;
        $.ajax({
            url: baseloaction + "/admin/agent/user/deleteAgentUser",
            data: {"agentUserId": agentUserId},
            type: "post",
            dataType:"json",
            success: function (result) {
                icModale(result.message);
                if(result.success){
                    if(pageNum >1 && rowNum==1){
                        getAgentUserList(pageNum-1);
                    }else {
                        getAgentUserList(pageNum);
                    }
                }
            },
            error: function (error) {
                icModale(0,"系统繁忙，请稍后重试");
            }
        })
    }

    /**
     * 拼接ID串
     * @param obj
     * @returns {*}
     */
    function obtainIds(obj) {
        if(null == obj || obj.size() == 0){
            icModale(0,"请选择要操作的数据");
            return null;
        }
        var agentUserids = "";
        $(obj).each(function (index,_input) {
            agentUserids += $(_input).data('id') + ",";
        });
        agentUserids = agentUserids.substring(0,agentUserids.length-1);
        return agentUserids;
    }
})
